package com.yitiao.data_algorithms.leetcode.editor.cn;
import com.yitiao.data_algorithms.leetcode.editor.cn.base.ListNode;

import java.util.ArrayList;
import java.util.List;

class RemoveNthNodeFromEndOfList{
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {

    public ListNode removeNthFromEnd(ListNode head, int n) {
        // 方便返回链表
        ListNode dummy = new ListNode(0, head);
        // 从dummy开始走，就不用特殊处理 head = [1], n = 1 这种情况
        head = dummy;
        ListNode tail = dummy;
        for (int i = 0; i < n; i++) {
            head = head.next;
        }
        while (head.next!=null){
            tail = tail.next;
            head = head.next;
        }
        tail.next = tail.next.next;
        return dummy.next;
    }


}
//leetcode submit region end(Prohibit modification and deletion)

}